package com.cinema;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import query.DataQuery;

import com.cinema.data.Adminusr;
import com.cinema.data.FilmInfo;
import com.cinema.data.Schedule;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

public class AddFilm extends HttpServlet{

	/**
	 * 
	 */
	public AddFilm(){
		super();
	}
	
	private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	
	private static final long serialVersionUID = 4010782057746594985L;
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req, resp);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		HttpSession s = request.getSession();
		Adminusr user = (Adminusr)s.getAttribute("adminuser");
		
		//if(user == null)
			//response.sendRedirect("admincp.html");
		if(false);
		else{
			Map<String, BlobKey> blobs = 	blobstoreService.getUploadedBlobs(request);
			BlobKey blobKey = blobs.get("imgLink");
			String imgLink = null;
			if(blobKey == null){
				response.sendRedirect("cp.jsp");
			}else{
				imgLink =  blobKey.getKeyString();
			}
			
			String filmname = request.getParameter("filmname");
			int rating = Integer.parseInt(request.getParameter("rating"));
			String shortInfo = request.getParameter("shortInfo");
			String description = request.getParameter("description");
			String fromday = request.getParameter("fromday");
			String today = request.getParameter("today");
			String[] schedule = request.getParameterValues("schedule");
			String trailerlink = request.getParameter("trailerlink");
			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			
			Schedule sched = new Schedule(schedule.length);
			Date[] date = new Date[schedule.length];
			for(int i = 0; i < schedule.length; i++){
				try {
					date[i] = formatter.parse(schedule[i]);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			sched.setDate(date);
			
			
			FilmInfo film = null;
			
			try {
				film = new FilmInfo(filmname, trailerlink, rating, description, 
						formatter.parse(fromday), formatter.parse(today), 
						shortInfo, imgLink, sched);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DataQuery query = new DataQuery();
			query.addFilm(film);
			response.setContentType("text/html");
			response.getWriter().println("successful");
		}
	}
}